package com.yt.app.api.action.pay.util;

import com.alibaba.fastjson.JSONObject;
import com.yt.app.api.util.Config;
import com.yt.core.MsgBean;
import org.apache.commons.lang3.StringUtils;
import org.jdom.JDOMException;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * Description
 * <p>
 * </p>
 * DATE 2017/10/18.
 *
 * @author liujunshi.
 */
public class WechatPayUtils {
    public static Map<String, String> weixinPrePay(String sn, BigDecimal totalAmount,
                                                   String description, String ip) {
        SortedMap<String, Object> parameterMap = new TreeMap<String, Object>();
        parameterMap.put("appid", PayCommonUtil.APPID);
        parameterMap.put("mch_id", PayCommonUtil.MCH_ID);
        parameterMap.put("nonce_str", PayCommonUtil.getRandomString(32));
        parameterMap.put("body",
                StringUtils.abbreviate(description.replaceAll(
                        "[^0-9a-zA-Z\\u4e00-\\u9fa5 ]", ""), 600));
        parameterMap.put("out_trade_no", sn);
        parameterMap.put("fee_type", "CNY");
        BigDecimal total = totalAmount.multiply(new BigDecimal(100));
        java.text.DecimalFormat df = new java.text.DecimalFormat("0");
        parameterMap.put("total_fee", df.format(total));
        parameterMap.put("spbill_create_ip", ip);
        parameterMap.put("notify_url", Config.apiUrl + "/pay/wechat/notify");
        parameterMap.put("trade_type", "APP");
        parameterMap.put("sign_type", "MD5");
        String sign = PayCommonUtil.createSign("UTF-8", parameterMap);
        parameterMap.put("sign", sign);
        String requestXML = PayCommonUtil.getRequestXml(parameterMap);
        //System.out.println(requestXML);
        String result = PayCommonUtil.httpsRequest(
                "https://api.mch.weixin.qq.com/pay/unifiedorder", "POST",
                requestXML);
        System.out.println(result);
        Map<String, String> map = null;
        try {
            map = PayCommonUtil.doXMLParse(result);
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }

    public static Map getPayParams(String orderNo, String description, String amount, String remoteIp) {
        Map<String, String> map = weixinPrePay(orderNo, new BigDecimal(amount), description, remoteIp);
        SortedMap<String, Object> parameterMap = new TreeMap<String, Object>();
        parameterMap.put("appid", PayCommonUtil.APPID);
        parameterMap.put("partnerid", PayCommonUtil.MCH_ID);
        parameterMap.put("prepayid", map.get("prepay_id"));
        parameterMap.put("package", "Sign=WXPay");
        parameterMap.put("noncestr", PayCommonUtil.getRandomString(32));
        parameterMap.put("timestamp", System.currentTimeMillis());
        String sign = PayCommonUtil.createSign("UTF-8", parameterMap);
        parameterMap.put("sign", sign);
        return parameterMap;
    }

    public static void main(String[] args) {
        Map map =WechatPayUtils.getPayParams("A00001232321", "体验商品信息", "0.01", "182.40.3.2");
        MsgBean msgBean = new MsgBean();
        msgBean.put("parameterMap", map);
        System.out.println(JSONObject.toJSON(msgBean.returnMsg()).toString());
    }
}
